#include "Map.h"
#include "Window.h"
#include "2DDevice.h"
#include "QuadRenderer.h"
#include "Main.h"
#include "Select.h"
#include "AstarPathFind.h"




void cMap::Restart()
{
	MapTile * CurrentNotWalkable;
	MapTile * OldTiles;
	int i,j,addr, counter = 0, color, LastLineStartColor, idx;
	QuadsOnScreenX = floor((Main.GetWindow().GetToolClientAreaSizeWidth() / QuadSize)+1.0f) - 2;
	QuadsOnScreenY = floor((Main.GetWindow().GetToolClientAreaSizeHeight() / QuadSize)+1.0f) - 2;
	if(MapTiles)
	{
		OldTiles = MapTiles;
		MapTiles = new MapTile[ QuadsOnScreenX * QuadsOnScreenY ];
		if(FirstNotWalkable)
		{
			CurrentNotWalkable = FirstNotWalkable;
			FirstNotWalkable = NULL;
			while(CurrentNotWalkable)
			{
				SetTileNotWalkable((CurrentNotWalkable->TilePosY * QuadsOnScreenX) + CurrentNotWalkable->TilePosX);
				CurrentNotWalkable = CurrentNotWalkable->NextNotWalkable;
			}
		}
		
		delete [] OldTiles;
		OldTiles = NULL;
	}else
	{
		MapTiles = new MapTile[ QuadsOnScreenX * QuadsOnScreenY ];
	}

	for(j = 0; j != QuadsOnScreenY; j++)
	{
		for(i = 0; i != QuadsOnScreenX; i++)
		{
			idx = (j * QuadsOnScreenX) + i;
			MapTiles[idx].TilePosX = i;
			MapTiles[idx].TilePosY = j;
			//Map[idx].Index = idx;
		}
	}



if( g_pVB != NULL )
	{
        g_pVB->Release();
		g_pVB = NULL;
	}

StartPos = D3DXVECTOR3(-(Main.GetWindow().GetToolClientAreaSizeWidth()/2.0f),(Main.GetWindow().GetToolClientAreaSizeHeight()/2.0f), 0.0f);
D3DXVECTOR3 EndPos((Main.GetWindow().GetToolClientAreaSizeWidth()/2.0f), -(Main.GetWindow().GetToolClientAreaSizeHeight()/2.0f), 0.0f);

//CUSTOMVERTEX g_Vertices[] =
    //{
        //{ -(Window->GetToolClientAreaSizeWidth()/2.0f),-(Window->GetToolClientAreaSizeHeight()/2.0f), 0.0f, 0xffff0000, },
       // {  (Window->GetToolClientAreaSizeWidth()/2.0f),-(Window->GetToolClientAreaSizeHeight()/2.0f), 0.0f, 0xff0000ff, },
        //{  0.0f, (Window->GetToolClientAreaSizeHeight()/2.0f), 0.0f, 0xffffffff, },
    //};
//QuadsOnScreenX = 5;
//QuadsOnScreenY = 5;
float x = (Main.GetWindow().GetToolClientAreaSizeHeight()/2.0f);
    // Create the vertex buffer.
if( FAILED( Main.GetTwoDDevice().GetDevice()->CreateVertexBuffer( QuadsOnScreenX * QuadsOnScreenY * NUM_VERTICES_IN_QUAD * sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
		MessageBox(0, "cMap::Restart()::CreateVertexBuffer failed", WINDOW_CAPTION,MB_OK); 
        return;
    }

    // Fill the vertex buffer.
    CUSTOMVERTEX* pVertices;
    if( FAILED( g_pVB->Lock( 0, QuadsOnScreenX * QuadsOnScreenY * NUM_VERTICES_IN_QUAD * sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) )
	{
		MessageBox(0, "cMap::Restart()::g_pVB->Lock failed", WINDOW_CAPTION,MB_OK); 
		return;
	}
   // memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
	D3DXVECTOR3 CurrentPos;
	D3DXVECTOR3 Pluss;

	LastLineStartColor = SECOND_COLOR;
	DWORD FColor=FIRST_COLOR;
	DWORD SColor=SECOND_COLOR;
	
	for(j=0; j != QuadsOnScreenY; j++)
	{
		if(LastLineStartColor == FIRST_COLOR)
		{
			FColor = SECOND_COLOR;
			SColor = FIRST_COLOR;
			LastLineStartColor = SECOND_COLOR;
		}else
		{
			SColor = SECOND_COLOR;
			FColor = FIRST_COLOR;
			LastLineStartColor = FIRST_COLOR;
		}
	    for(i=0; i != QuadsOnScreenX; i++)
		{
			if(i==0)
			{
				color = FColor;
			}else
			{
				if(color == FColor)
				{
					color = SColor;
				}else
				{
					color = FColor;
				}
			}
			Pluss = ZERO_VECTOR;
			Pluss.x = i * QuadSize;
			Pluss.y = -(j * QuadSize);
			CurrentPos = StartPos + Pluss;
			addr = ((i + (QuadsOnScreenX * j)) * NUM_VERTICES_IN_QUAD);
			
			pVertices[addr].pos = CurrentPos;
			pVertices[addr + 1].pos = CurrentPos + D3DXVECTOR3(QuadSize, 0.0f, 0.0f);
			pVertices[addr + 2].pos = CurrentPos + D3DXVECTOR3(QuadSize, -QuadSize, 0.0f);

			pVertices[addr + 3].pos = CurrentPos;
			pVertices[addr + 4].pos = CurrentPos + D3DXVECTOR3(QuadSize, -QuadSize, 0.0f);
			pVertices[addr + 5].pos = CurrentPos + D3DXVECTOR3(0.0f, -QuadSize, 0.0f);
			
			pVertices[addr].color = color;
			pVertices[addr + 1].color = color;
			pVertices[addr + 2].color = color;
			pVertices[addr + 3].color = color;
			pVertices[addr + 4].color = color;
			pVertices[addr + 5].color = color;
			
		}
		
	}
	g_pVB->Unlock();

}
cMap::cMap(cMain & Main): Main(Main),FirstNotWalkable(NULL), g_pVB(NULL), MapTiles(NULL), LastNotWalkable(NULL)
{
	Restart();
}

cMap::~cMap()
{

}

void cMap::Render()
{
		Main.GetTwoDDevice().GetDevice()->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
        Main.GetTwoDDevice().GetDevice()->SetFVF( D3DFVF_CUSTOMVERTEX );
        Main.GetTwoDDevice().GetDevice()->DrawPrimitive( D3DPT_TRIANGLELIST, 0, (QuadsOnScreenX * QuadsOnScreenY) * NUM_TRIANGLES_IN_QUAD);
}

bool cMap::GetTileIsWalkable(int id)
{
	if(MapTiles[id].Walkable == WALKABLE)
	return true;
	return false;
}

void cMap::SetTileNotWalkable(int id)
{
	if(MapTiles[id].Walkable)
	{
		MapTiles[id].Walkable = false;
		if(FirstNotWalkable == NULL)
		{
			FirstNotWalkable = &MapTiles[id];
			LastNotWalkable = FirstNotWalkable;
			NumNotWalkableTiles = 1;
		}else
		{
			LastNotWalkable->NextNotWalkable = &MapTiles[id];
			LastNotWalkable = LastNotWalkable->NextNotWalkable;
			NumNotWalkableTiles++;
		}
	}
}
void cMap::RenderNotWalkable()
{
	D3DXVECTOR3 CurrentPos;
	MapTile * CurrentNotWalkable = FirstNotWalkable;
	while(CurrentNotWalkable)
	{
		CurrentPos = ZERO_VECTOR;
		CurrentPos.x = CurrentNotWalkable->TilePosX * QuadSize;
		CurrentPos.y = -(CurrentNotWalkable->TilePosY * QuadSize);
		Main.GetQuadRenderer().AddQuadToRender(CurrentPos,NOT_WALKABLE_SELECTION_COLOR, QuadSize);
		CurrentNotWalkable = CurrentNotWalkable->NextNotWalkable;
	}
}